# -*- coding: utf-8 -*-
import scrapy
from scrapy_redis.spiders import RedisSpider

import whucs.settings as setting
from ..items import WhucsItem


class LectureSpider(RedisSpider):
    name = "lecture"

    redis_key = setting.REDIS_LECTURE_URLS_KEY

    custom_settings = {
        'ITEM_PIPELINES': {
            'whucs.pipelines.LectureDetailPipeline': 300
        }
    }

    def make_requests_from_url(self, url):
        return scrapy.Request(url)

    def parse(self, response):
        """
        解析Item
        :param response:
        :return:
        """
        item = WhucsItem()

        container = response.xpath("//dd[@class='info']")
        infos = container[0].xpath('string(.)').extract()[0].split("\n")
        for info in infos:
            str = info.encode('utf8').strip()
            if len(str) < 5:
                infos.remove(info)

        item['url'] = response.url
        item['title'] = infos[0]
        item['time'] = infos[3]
        item['place'] = infos[4]
        item['speaker'] = infos[1]
        item['speaker_info'] = infos[5]
        item['summary'] = infos[6]
        item['inviter'] = infos[7]

        yield item
